<?php

namespace App\System\Cis\Repositories;

use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Cis\Model\CisAdmin;
use App\System\Cis\Model\CisAdminRole;
use App\System\Cis\Model\CisModule;


class CisAdminRoleRepository extends Repository
{
    use SinglePattern;

    /*
     * 职务列表
     *@$param $page 页数
     * @$param $limit 条数
     * @return Collection
     * @auth Hjj
     */

    public function getlist($page = 1, $limit = 10)
    {
        return CisAdminRole::paginate($limit, array('*'), 'page', $page);
    }

    /*
     * 新增管理员
     * @param array $data 要保存的数据
     * @author Hjj
     */
    public function addData($data)
    {
        $res = CisAdminRole::create($data);
        if ($res) {
            $msg = '操作成功！';
        } else {
            $msg = '操作失败！';
        }
        return ['data' => $res, 'message' => $msg];
    }

    /*
     * 编辑管理员
     * @param $id 管理员ID
     * @param array $updata 修改的数据
     * @author Hjj
     */
    public function editData($id, $updata)
    {
        if (empty($id)) {
            return false;
        }
        $admin = CisAdminRole::find($id);
        $admin->name = $updata['name'];
        $admin->action_list = $updata['action_list'];
        $bool = $admin->save();
        if ($bool) {
            $msg = '操作成功！';
        } else {
            $msg = '操作失败！';
        }
        return ['data' => $bool, 'message' => $msg];
    }

    /*
     * 获取职务详情
     * @param $id 职务ID
     * @author Hjj
     */
    public function getDetail($id)
    {
        return CisAdminRole::where('id', $id)->first();
    }

    /*
     * 职务列表(不分页)
     * @param $super 1获取超级管理员角色 0 不获取
     * @return Collection
     * @author Hjj
     */

    public function getAllList($super = 0)
    {
        if ($super) {
            return CisAdminRole::get();
        } else {
            return CisAdminRole::where('id', '!=', 1)->get();
        }
    }

    /*
     * 获取职务的权限模块
     * @param $id 职务ID
     * @author Hjj
     */
    public function getModules($id)
    {
        if (!$id) {
            return false;
        }
        $info = CisAdminRole::where('id', $id)->first();
        $list = [];
        if ($info) {
            if ($info->action_list == 'all') {
                $list = 'all';
            } else {
                $moduleArr = explode(',', $info->action_list);
                $children = CisModule::whereIn('id', $moduleArr)->get();
                foreach ($children as $v) {
                    $parent = CisModule::where('id', $v->parent_id)->first();
                    $grandparent = CisModule::where('id',$parent->parent_id)->first();
                    $list[$grandparent->name][$parent->name][] = $v;
                }
            }
        }
        return $list;
    }

    /*
     * 删除职务
     * @param $id 职务ID
     * @author Hjj
     */
    public function delData($id)
    {
        if (!$id) {
            return false;
        }
        $admins = CisAdmin::where('role_id', $id)->get();
        if (!$admins->isEmpty()) {
            $bool = 0;
            $msg = '该职务下有管理员，不能删除！';
        } else {
            $bool = CisAdminRole::where('id', $id)->first()->delete();
            if ($bool) {
                $msg = '操作成功！';
            } else {
                $msg = '操作失败！';
            }
        }
        return ['data' => $bool, 'message' => $msg];
    }
}